package com.example.zyf.sortlistviewandsidebar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private SideBar sidebar;
    private ListView lv_sort;
    private TextView tv_list_dialog;

    String[] citys = new String[]{"河南", "郑州", "荥阳", "登封", "新郑", "中牟", "密县", "巩义", "洛阳", "宜阳", "洛宁", "新安", "伊川", "汝阳", "嵩县", "偃师", "孟津", "栾川", "开封", "兰考", "尉氏", "杞县", "通许", "平顶山", "汝州", "鲁山", "郏县", "叶县", "宝丰", "焦作", "沁阳", "博爱", "修武", "武陟", "温县", "孟县", "鹤壁", "淇县", "浚县", "新乡", "安阳", "汤阴", "内黄", "滑县", "林州", "濮阳", "台前", "南乐", "清丰", "范县", "许昌", "长葛", "禹州", "鄢陵", "襄城", "漯河", "三门峡", "灵宝", "渑池", "卢氏", "南阳", "镇平", "方城", "社旗", "淅川", "新野", "唐河", "邓州", "西峡", "南召", "信阳", "固始", "周口", "西华", "驻马店", "济源", "商丘", "睢县", "民权", "虞城", "柘城", "宁陵", "夏邑", "永城"};

    List<City> cityList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        sidebar.setTextView(tv_list_dialog);

        sidebar.postDelayed(new Runnable() {
            @Override
            public void run() {
//                Toast.makeText(MainActivity.this, CharacterParser.getInstance().getSelling("鄢陵"), Toast.LENGTH_SHORT).show();
//                Log.i(TAG, "" + CharacterParser.getInstance().getSelling("庐州，长安，兰陵"));
            }
        }, 3000);

        for (int i = 0; i < citys.length; i++) {
            City city = new City();
            city.setName(citys[i]);
            char s = CharacterParser.getInstance().getSelling(citys[i]).charAt(0);
            if (s >= 'a' && s <= 'z') {//判断字符是否在a-z之间（小写）
                s = (char) (s - 32);               //如果为小写则转换为相应大写,赋值给ch
            }
            city.setCityInitial(s);
            cityList.add(city);
        }

        //冒泡排序   int ascii = (int) a;   ascii码   65A----90Z
        for (int i = 0; i < cityList.size() - 1; i++) {
            for (int j = 0; j < cityList.size() - 1 - i; j++) {
                if ((int) cityList.get(j).getCityInitial() > (int) cityList.get(j + 1).getCityInitial()) {
                    Collections.swap(cityList, j, j + 1);
                }
            }
        }

        Log.i(TAG, "");

        for (int i = 0; i < cityList.size(); i++) {
            Log.i(TAG,"------city initial---- "+cityList.get(i).getName()+" "+CharacterParser.getInstance().getSelling(cityList.get(i).getName()));
        }

        final SortAdapter adapter = new SortAdapter(this, cityList);
        lv_sort.setAdapter(adapter);


        sidebar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                int position = adapter.getPositionForSection(s.charAt(0));
//                Log.i(TAG,"------position--------- "+position);
                if (position != -1)
                    lv_sort.setSelection(position);
            }
        });

    }


    public void initView() {
        sidebar = (SideBar) findViewById(R.id.sidebar);
        lv_sort = (ListView) findViewById(R.id.lv_sort);
        tv_list_dialog = (TextView) findViewById(R.id.tv_list_dialog);
    }


}
